package com.loubobooo.personal.blog.controller.portal;

import com.loubobooo.personal.blog.entity.Article;
import com.loubobooo.personal.blog.entity.Comment;
import com.loubobooo.personal.blog.service.ArticleService;
import com.loubobooo.personal.blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller public class CommentController {
    @Autowired private CommentService commentService;

    @Autowired private ArticleService articleService;

    //添加评论
    @RequestMapping(value = "/comment/insert", method = { RequestMethod.POST })
    public void insertComment(
            HttpServletRequest request, Comment comment){
        //添加评论
        comment.setCommentCreateTime(new Date());
        commentService.insertComment(request, comment);
        //更新文章的评论数
        Article article = articleService.getArticleById(null, comment.getCommentArticleId());
        articleService.updateCommentCount(article.getArticleId());
    }

}
